﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class submitbug : System.Web.UI.Page
{
    private string ProductID;
    private string ProductVersion;
    private string connectionString = WebConfigurationManager.ConnectionStrings["bugger"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

        ProductID = Request.QueryString["ProductID"];
        ProductVersion = Request.QueryString["ProductVersion"];

        SqlConnection userConnection = new SqlConnection(connectionString);
        userConnection.Open();
        string sqlquery = "SELECT productname FROM products WHERE productid = ProductID";
        SqlCommand getproductname = new SqlCommand(sqlquery, userConnection);

        SqlDataReader readproductname;

        if (this.IsPostBack == false)
        {

            SqlConnection productConnection = new SqlConnection(connectionString);
            string selectSql = "SELECT productname, productid FROM products";

            productConnection.Open();

            SqlCommand selectCommand = new SqlCommand(selectSql, productConnection);

            SqlDataReader productReader;
            productReader = selectCommand.ExecuteReader();

            while (productReader.Read())
            {
                ListItem newItem = new ListItem();
                newItem.Text = productReader["productname"].ToString();
                newItem.Value = productReader["productid"].ToString();
                lstProductName.Items.Add(newItem);
            }

            lstProductName.ClearSelection();

            productReader.Close();
            productConnection.Close();

        }


        if (ProductID != null && ProductVersion != null)
        {
            readproductname = getproductname.ExecuteReader();
            readproductname.Read();
            txtbox_product.Text = readproductname["productname"].ToString();
            txtbox_version.Text = ProductID;
            lstProductName.Visible = false;
            lstProductVersion.Visible = false;
            txtbox_product.ReadOnly = true;
            txtbox_version.ReadOnly = true;
        }
        else
        {
            txtbox_product.Visible = false;
            txtbox_version.Visible = false;

        }
    }
    protected void lstProductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection versionConnection = new SqlConnection(connectionString);
        string selectSql = "SELECT productversion FROM version WHERE productid = @productid ORDER BY productid ASC";

        versionConnection.Open();

        SqlCommand selectCommand = new SqlCommand(selectSql, versionConnection);
        int pid = Convert.ToInt32(lstProductName.SelectedItem.Value);
        selectCommand.Parameters.AddWithValue("@productid", pid);

        SqlDataReader versionReader;
        versionReader = selectCommand.ExecuteReader();

        lstProductVersion.Items.Clear();

        while (versionReader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = versionReader["productversion"].ToString();
            newItem.Value = versionReader["productversion"].ToString();

            lstProductVersion.Items.Add(newItem);
        }

        versionReader.Close();
        versionConnection.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string whoreport;
        string userabc = Session["User"].ToString();

        SqlConnection Connection = new SqlConnection(connectionString);
        string selectusersql = "SELECT userid FROM users WHERE username = @userabc";

        Connection.Open();

        SqlCommand selectCommand = new SqlCommand(selectusersql, Connection);

        selectCommand.Parameters.AddWithValue("@userabc", userabc);

        SqlDataReader userReader;
        userReader = selectCommand.ExecuteReader();

        userReader.Read();

        whoreport = userReader["userid"].ToString();



        userReader.Close();
        Connection.Close();



        SqlConnection submitConnection = new SqlConnection(connectionString);
        submitConnection.Open();



        string sqlInsert = "INSERT INTO bugs (reporterid, productid, productversion, type, description, reportdate, priority)";
        sqlInsert += "VALUES (@reporterid, @productid, @productversion, @type, @description, @reportdate, @priority)";
        SqlCommand InsertCommand = new SqlCommand(sqlInsert, submitConnection);

        string whatproductid, whatproductversion;
        if (txtbox_product.Visible == true && txtbox_version.Visible == true)
        {
            whatproductid = ProductID;
            whatproductversion = txtbox_version.Text;
        }
        else
        {
            whatproductid = lstProductName.Text;
            whatproductversion = lstProductVersion.Text;
        }


        InsertCommand.Parameters.AddWithValue("@reporterid", whoreport);
        InsertCommand.Parameters.AddWithValue("@productid", whatproductid);
        InsertCommand.Parameters.AddWithValue("@productversion", whatproductversion);
        InsertCommand.Parameters.AddWithValue("@type", Type.Text);
        InsertCommand.Parameters.AddWithValue("@description", Description.Text);
        InsertCommand.Parameters.AddWithValue("@reportdate", DateTime.Now);
        InsertCommand.Parameters.AddWithValue("@priority", Priority.Text);

        if (InsertCommand.ExecuteNonQuery() != 0)
        {
            Response.Write("hehehe");
        }
        else
            Response.Write("nooooooooooo");
        submitConnection.Close();

    }
}